﻿namespace ReplayDataFile
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Runtime.InteropServices;

    public class DataImporter
    {
        public static List<DataRecord> ImportFromCompactFile(string fileName, out List<int> verList)
        {
            List<DataRecord> list = new List<DataRecord>();
            verList = new List<int>();
            if (File.Exists(fileName))
            {
                try
                {
                    string decompressedFile = fileName + ".tmp";
                    GZipUtil.DecompressFile(fileName, decompressedFile);
                    FileInfo info = new FileInfo(decompressedFile);
                    if (info.Length < 0x100L)
                    {
                        return list;
                    }
                    FileStream stream = new FileStream(decompressedFile, FileMode.Open, FileAccess.Read, FileShare.None);
                    byte[] buffer = new byte[0x100];
                    stream.Read(buffer, 0, buffer.Length);
                    BinaryReader reader = new BinaryReader(new MemoryStream(buffer));
                    if (reader.ReadUInt64() != DataExporter.Fileflag)
                    {
                        stream.Close();
                        return list;
                    }
                    byte[] buffer2 = new byte[info.Length - buffer.Length];
                    stream.Read(buffer2, 0, buffer2.Length);
                    BinaryReader br = new BinaryReader(new MemoryStream(buffer2));
                    DataRecord preRecord = null;
                    RecordUnZipper zipper = new RecordUnZipper();
                    int num2 = reader.ReadInt32();
                    for (int i = 0; i < num2; i++)
                    {
                        int num4 = br.ReadInt32();
                        short num5 = br.ReadInt16();
                        DataRecord item = new DataRecord();
                        if (!item.FromByteBuffer(br, buffer2, preRecord))
                        {
                            break;
                        }
                        item.DataType = num4;
                        item.DataVersion = num5;
                        preRecord = item;
                        list.Add(item);
                        if (!verList.Contains(item.DataVersion))
                        {
                            verList.Add(item.DataVersion);
                        }
                        zipper.UnZip(item);
                    }
                    stream.Close();
                    File.Delete(decompressedFile);
                }
                catch (Exception exception)
                {
                    Trace.WriteLine("ImportRecordList error! ", exception.Message);
                }
            }
            return list;
        }
    }
}

